package meijuli.com.join51.business.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.fragment.BindBankFragment;
import meijuli.com.join51.business.fragment.ShowBankFragment;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import meijuli.com.join51.business.model.BankMsg;
import meijuli.com.join51.business.service.ApiService;
import retrofit2.Call;

/**
 *
 *
 * @author wenqin 2017-05-05 12:00
 */

public class BankActivity extends BaseActivity {
    TextView mToolbarTitle;
    TextView mTextMenu;
    Toolbar mToolbar;
    private Call<HttpResponse<BankMsg>> call;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, BankActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_fragment);
        mToolbarTitle = findViewById(R.id.toolbar_title);
        mTextMenu = findViewById(R.id.text_menu);
        mToolbar = findViewById(R.id.toolbar);
        initToolBar();
        //重新启动BankActivity
        if (savedInstanceState != null) {
            BankActivity.start(BankActivity.this);
            finish();
            return;
        }

        showLoadingDialog();
        call = ApiManager.get().getApiService(ApiService.class).getBankCard();
        HttpRequest.getRequest().autoRecycle(this).start(call, new ResponseCallback<BankMsg>() {

            @Override
            public void onSuccess(String url, BankMsg data) {
                dismissDialog();
                addFragment(ShowBankFragment.newInstance(data));
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                dismissDialog();
                if (errorCode == 7777) {
                    addFragment(BindBankFragment.newInstance(true));
                    return;
                }
            }
        });
    }

    private void initToolBar() {
        mToolbar.setTitle("");
        mToolbarTitle.setText("绑定银行卡");
        mToolbar.setNavigationIcon(R.mipmap.icon_back_white);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!getSupportFragmentManager().popBackStackImmediate()) {
                    finish();
                }
            }
        });
    }

    public void setToolBarTitle(String s) {
        mToolbarTitle.setText(s);
    }

}
